package com.cn.barcode;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.IHardwareService;

import com.cn.barcode.ScanManager.OnScanOverListener;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */

	private SurfaceView mCameraView;
	private ScanManager mScanManager;
	private View centerView;
	private Handler mHandler = null;
	private Button queryButton = null;
	private Button openButton = null;
	private EditText mInputContent = null;
	private TextView mTextView;
	private String mPhoneNumber = null;
	private String mInputContentValue = null;
	private boolean checkSetting;
	private static final int REQ_SYSTEM_SETTINGS = 0;
	private boolean juge = false;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		setContentView(R.layout.main);

		centerView = (View) this.findViewById(R.id.centerView);
		mCameraView = (SurfaceView) this.findViewById(R.id.cameraView);
		mInputContent = (EditText) findViewById(R.id.input_content);
		queryButton = (Button) findViewById(R.id.query_button);
		openButton = (Button) findViewById(R.id.open);
		mTextView = (TextView) findViewById(R.id.imei);
		mScanManager = new ScanManager(this, mCameraView.getHolder());
		mScanManager.setScanOverListener(mOnScanOverListener);
		initData();

		mHandler = new Handler();
		mHandler.postDelayed(mScan, 500);
		openButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (juge) {
					setFlashlightEnabled(false);
					openButton.setBackgroundResource(R.drawable.pic3);
					juge = false;
				} else {
					setFlashlightEnabled(true);
					openButton.setBackgroundResource(R.drawable.pic5);
					juge = true;
				}

			}
		});
		queryButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mInputContentValue = mInputContent.getText().toString();
				if (mInputContentValue != null) {
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, RpsInfo.class);
					intent.putExtra("resultString", mInputContentValue);
					startActivity(intent);
				} else {
					Toast.makeText(MainActivity.this, "未扫描到二维码",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	private OnScanOverListener mOnScanOverListener = new OnScanOverListener() {

		@Override
		public void onScanOver(String resultType, String resultText) {
			Toast.makeText(getApplicationContext(), R.string.scan_success, 1000)
					.show();
			// txtScanResult.setText("type: " + resultType +"\r\n" +" text " +
			// resultText);
			mInputContent.setText(resultText);
		}

		@Override
		public void onScanFailed() {
			// txtScanResult.setText("Scanning");
		}

		@Override
		public void onBeforeScan(Bitmap bitMap) {
			// 显示灰度图
			// imgView.setImageBitmap(bitMap);
		}
	};

	private Runnable mScan = new Runnable() {

		@Override
		public void run() {
			SharedPreferences jugeneedsetting = getSharedPreferences("setting",
					0);
			checkSetting = jugeneedsetting.getBoolean("jugeneedsetting", true);
			if (checkSetting) {
				Intent setting = new Intent(MainActivity.this, Setting.class);
				startActivityForResult(setting, REQ_SYSTEM_SETTINGS);
			}
			mScanManager.initalRect(centerView.getLeft(), centerView.getTop(),
					centerView.getRight(), centerView.getBottom(),
					mCameraView.getWidth(), mCameraView.getHeight());
			// mScanManager.beginScan();
		}
	};

	public String getPhoneNumber(Context context) {
		TelephonyManager mTelephonyMgr;
		mTelephonyMgr = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return mTelephonyMgr.getDeviceId();
	}

	private void initData() {
		mPhoneNumber = getPhoneNumber(this);
		if (mPhoneNumber != null) {
			mTextView.setText(mPhoneNumber);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		super.onCreateOptionsMenu(menu);
		menu.add(0, 0, 0, "设置").setIcon(R.drawable.setting)
				.setAlphabeticShortcut('A');
		return true;
	}

	@Override
	protected void onPause() {
		Log.d("test", "onPause");
		super.onPause();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			Intent settingintent = new Intent();
			settingintent.setClass(this, Setting.class);
			startActivity(settingintent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void setFlashlightEnabled(boolean isEnable) {
		if(isEnable){
			mScanManager.turnONLight();
		}else {
			mScanManager.turnOffLight();
		}
	}

}